import { defineStore } from "pinia";
import { ref } from "vue";
import { UserInfoVo, LoginVo } from "@/types/login";
import { login } from "@/api/login.api";
import { setJwt, setUserCache } from "@/utils/storage";
import { useRouter } from "vue-router";
export const useLoginStore = defineStore("loginInfoStore", () => {
  const router = useRouter();
  const jwt = ref("");
  const userInfo = ref<UserInfoVo>(new UserInfoVo());
  const loginInf = async (data: LoginVo) => {
    const res = await login(data);
    if (res.code === 200) {
      jwt.value = res.data && res.data.token ? res.data.token : "";
      userInfo.value = res.data && res.data.userInfo ? res.data.userInfo : {};
      setJwt(jwt.value);
      setUserCache(userInfo.value);
      router.replace("/");
    }
  };
  return {
    jwt,
    loginInf,
    userInfo,
  };
});
